const sequelize = require("./db");
const { DataTypes } = require("sequelize");

const Article = sequelize.define("Article", {
  title: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  coverImg: {
    type: DataTypes.STRING,
    allowNull: true,
  },
  des: {
    type: DataTypes.STRING(500),
    allowNull: false,
  },
  content: {
    type: DataTypes.TEXT,
    allowNull: false,
  },
  url: {
    type: DataTypes.STRING,
    allowNull: true,
  },
  owner: {
    type: DataTypes.STRING,
    defaultValue:'蚂蚁宝藏网'
  },
  has:{
    type: DataTypes.STRING,
    defaultValue:'蚂蚁宝藏网'
  },
  views: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
  },
  tags: {
    type: DataTypes.STRING,
    allowNull:false,
    get(){
      return this.getDataValue('tags') && this.getDataValue('tags').split(',').map(Number)
    },
    set(value){
      return this.setDataValue('tags',value.join(','))
    }
  },
  isRecommend: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
  },
});

module.exports = Article;
